package com.bootdo.collection.controller;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.collection.domain.PictureDO;
import com.bootdo.collection.domain.PictureSetDO;
import com.bootdo.collection.domain.SourceStationDO;
import com.bootdo.collection.service.PictureService;
import com.bootdo.collection.utils.JedisUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 图集控制器
 */
@RequestMapping("/collection/picture")
@Controller
public class PictureController {
    @Autowired
    private PictureService pictureService;

    @PostMapping("/getByPictureSetId")
    @ResponseBody()
    List<PictureDO> getPicturesBySetId(@RequestParam("pictureSetId") int pictureSetId, @RequestParam("stationId") Integer stationId) {
        return pictureService.getPicturesBySetId(pictureSetId, stationId);
    }

    @PostMapping("/deleteByPictureId")
    @ResponseBody
    R delete(@RequestParam int pictureId) {
        return pictureService.deleteByPictureId(pictureId);
    }


    /**
     * 上传图集到ftp和对应的站点后台
     * @param pictureSetId 图集id
     * @param title 后台编辑过的图集标题
     * @param station 推送的站点id
     * @param pictureIds 图集内删除或添加后的所有图片id
     */
    @PostMapping("/uploadPicture")
    @ResponseBody
    public void upload(@RequestParam int pictureSetId, @RequestParam String title, @RequestParam(value = "station[]") int[] station, @RequestParam(value = "pictureIds[]") int[] pictureIds) {
        //因为是后台上传，无法立即获得上传结果，所以不再返回处理结果
        Logger logger = LoggerFactory.getLogger(PictureController.class);
        try {
            pictureService.upload(pictureSetId, title, station, pictureIds);
        } catch (Exception e) {
            logger.error("图片推送出错");
            e.printStackTrace();
        }
    }

    /**
     * 将图片添加到伪原创库
     * @param keyTitle     键
     * @param pictureSetId 图集id
     * @param stationId    站点id
     */
    @PostMapping("/addLibrary")
    @ResponseBody
    public void addLibrary(String keyTitle, int pictureSetId, int stationId) {
        PictureSetDO pictureSetDO = new PictureSetDO();
        pictureSetDO.setTitle(keyTitle);
        pictureSetDO.setPictureSetId(pictureSetId);
        pictureSetDO.setSourceStationId(stationId);
        pictureService.addLibrary(pictureSetDO);
    }

    /**
     * 打开伪原创库
     *
     * @param key   键
     * @param model 将数据带回页面对象
     * @return
     */
    @GetMapping("/openLibrary/{keyTitle}")
    public String openLibrary(@PathVariable("keyTitle") String key, Model model) {
        JedisUtil.init();
        String pictureJson = JedisUtil.get(key);
        if (StringUtils.isNotBlank(pictureJson)) {
            List<PictureDO> pictureDOS = JSONObject.parseArray(pictureJson, PictureDO.class);
            model.addAttribute("pictureDOS", pictureDOS);
        }
        return "collection/picture/library";
    }

    /**
     * 从伪原创中选出图片加入图集
     * @param pictureSetId 图集id
     * @param ids          选出的图片
     * @param keyTitle     伪原创库的键
     * @return
     */
    @PostMapping("/addAtlas")
    @ResponseBody
    public List<PictureDO> addAtlas(int pictureSetId, @RequestParam("ids[]") int[] ids, int stationId, String keyTitle) {
        return pictureService.addAtlas(pictureSetId, ids, keyTitle);
    }

    /**
     * 删除伪原创库中的某些图片
     *
     * @param keyTitle 伪原创库中键
     * @param ids      删除的图片id
     */
    @GetMapping("/deleteFromLibrary")
    @ResponseBody
    public void deleteFromLibrary(String keyTitle, @RequestParam("ids[]") int[] ids) {
        pictureService.deleteFromLibrary(keyTitle, ids);
    }

}
